<!-- 机构信息 -->
<template>
	<view class="container">
		<view class="c-form">
			<view class="form-btn" v-if="applyStatus">
				<text @click="issueForm" v-if="status!='org'">提交</text>
				<text @click="saveForm" v-else-if="!isDisabled">提交</text>
			</view>
			<view class="form-btn" v-else>
				<text v-if="isDisabled" @click="isDisabled=false">编辑</text>
				<text v-else @click="navigateBack">取消</text>
				<text @click="saveForm">保存</text>
			</view>
			
			<view class="form-alert" v-if="applyStatus">
				<u-alert :title="alertText" type="warning" :description="description"></u-alert>
			</view>
			<view class="form-info">
				<u-form :model="orgInfo">
					<u-form-item label="机构全程">
						<u-input placeholder="机构名称" v-model="orgInfo.name" :disabled="isDisabled"></u-input>
					</u-form-item>
					<u-form-item label="机构地址">
						<u-textarea v-model="orgInfo.address" placeholder="机构地址" autoHeight :disabled="isDisabled"></u-textarea>
					</u-form-item>
					<u-form-item label="机构负责人">
						<u-input placeholder="机构负责人姓名" v-model="orgInfo.resp_name" :disabled="isDisabled"></u-input>
					</u-form-item>
					<u-form-item label="负责人电话">
						<u-input placeholder="机构负责人联系电话" v-model="orgInfo.resp_mobile" disabled></u-input>
					</u-form-item>
					<u-form-item label="开设科目">
						<u-textarea v-model="orgInfo.subject_name" placeholder="机构开设所有科目" autoHeight :disabled="isDisabled"></u-textarea>
					</u-form-item>
					<u-form-item label="机构照片">
						<view class="">
							<u-upload width="200rpx" height="200rpx" :uploadIconColor="'#666'" :uploadIcon="'plus'"
								:fileList="filePath" :previewFullImage="true" @afterRead="afterRead" @delete="deletePic"
								name="1" :maxCount="1"></u-upload>
						</view>
					</u-form-item>
					<u-form-item>
						<u-button text="保存" type="success" @click="saveForm"></u-button>
					</u-form-item>
				</u-form>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		name: "",
		data() {
			return {
				// 是否申请中
				applyStatus:false,
				// 审核原因
				description: null,
				// 审核提示
				alertText: "完善所有信息后提交申请",
				// 是否禁填
				isDisabled: false,
				// 照片
				filePath:[],
				// 用户身份
				status: this.$store.state.status,
				// 机构信息
				orgInfo: {
					user_id: '',
					name: null,
					img: null,
					subject_name: null,
					resp_name: null,
					resp_mobile: null,
					address: null
				}
			}
		},
		onLoad() {
			if(this.status=='org'){
				this.getOrgInfo()
			}else{
				this.applyStatus = true
				this.orgInfo.resp_mobile = uni.getStorageSync("userInfo").mobile
			}
		},
		methods: {
			// 重新提交机构申请信息
			saveForm(){
				this.$http.updateOrgInfo(this.orgInfo).then(res=>{
					this.getOrgInfo()
				})
			},
			// 获取机构信息
			getOrgInfo(){
				this.$http.getOrgInfo().then(res=>{
					this.applyStatus = false
					this.orgInfo = res
					this.filePath = [{url:this.baseURL + res.img}]
					if(!uni.getStorageInfoSync("userInfo").identityData){
						this.getUserInfo()
					}
				}).catch(err => {
					this.description = null
					this.applyStatus = true
					this.isDisabled = true
					this.alertText = err.msg
					if(err.data.check_remarks){
						this.description = "驳回原因:" + err.data.check_remarks
						this.isDisabled = false
					}
					this.orgInfo = err.data.organData
					this.filePath = [{url:this.baseURL + err.data.organData.img}]

				})
			},
			// 图片上传后
			afterRead(e) {
				// 微信小程序不支持多张图片上传
				uni.uploadFile({
					url: this.baseURL + '/api/common/upload',
					filePath: e.file.url,
					name: 'file',
					header: {
						"token": this.$store.state.token
					},
					success: (res) => {
						if (res.statusCode == 200) {
							let result = JSON.parse(res.data)
							let {
								data: {
									fullurl,
									url
								}
							} = result
							this.orgInfo.img = url
							this.filePath = [{url:fullurl}]
						} else {
							let result = JSON.parse(res.data)
							uni.$u.toast(result.msg)
						}

					},
					fail: (err) => {
						uni.$u.toast(err)
					}
				})
			},
			// 删除图片
			deletePic(e,code) {
				if(this.isDisabled) return
				this.filePath=[]
			},
			// 日期选择
			datePickerConfirm(obj) {
				this.dateShow = false
				this.orgInfo.birth = uni.$u.timeFormat(obj.value)
			},
			// 保存完善信息
			issueForm() {
				this.$http.saveOrgInfo(this.orgInfo).then(res=>{
					this.applyStatus = true
					this.getOrgInfo()
					this.getUserInfo()
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		.c-form {
			padding-top: 100rpx;
		
			.form-btn {
				width: 100%;
				height: 100rpx;
				background-color: #ddd;
				position: fixed;
				top: 0;
				left: 0;
				z-index: 9999;
				display: flex;
				justify-content: flex-end;
				align-items: center;
		
				&>text {
					font-size: 32rpx;
					font-weight: bold;
					padding: 0 20rpx;
				}
			}
		}
	}
</style>
